// Appendix2.10

log using Appendix2.10.log, replace

use "C:\Users\sbstjp\OneDrive - Cardiff University\Transgender IAT.public.2023.dta" // Project Implicit Demo Website Datasets, Transgender IAT, Last Updated: 2025-01-13 10:29 PM. Date accessed: March 09, 2025. SPSS version of the dataset downloaded and then, without alterations, converted to a Stata file. 

// Create the scale
*Standardize items in the scale from 1-2 - this puts the scale on the same scale as others in the book and avoids 0, for reasons outlined in next step
foreach var in transphobia1 transphobia2 transphobia3 transphobia4 transphobia5 transphobia6 transphobia7 transphobia8 transphobia9 {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in stransphobia1 stransphobia2 stransphobia3 stransphobia4 stransphobia5 stransphobia6 stransphobia7 stransphobia8 stransphobia9 {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreTP = 0
gen count_nonzeroTP = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in stransphobia1 stransphobia2 stransphobia3 stransphobia4 stransphobia5 stransphobia6 stransphobia7 stransphobia8 stransphobia9 {
    replace total_scoreTP = total_scoreTP + `var'
    replace count_nonzeroTP = count_nonzeroTP + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen TPScale = .
replace TPScale = total_scoreTP / count_nonzeroTP if count_nonzeroTP > 0

* Reverse so social justice values are coded high
qui sum TPScale
gen NonTransphobia = r(max) + 1 - TPScale

// Demographics
*This is a convenience sample and under 18s have completed it. But we're studying voters so drop under 18s.
drop if birthyear>2005 
gen age = 2023 - birthyear 
*Drop as probably misreported age
drop if age>100
 
* Rename variables and create dummies
rename birthSex FemaleGender

gen Graduate=.
replace Graduate=0 if inrange(edu_14, 1, 6)
replace Graduate=1 if inrange(edu_14, 7, 14)

gen BIPOC=.
replace BIPOC=0 if raceomb_002==6
replace BIPOC=1 if inrange(raceomb_002, 1, 5)
replace BIPOC=1 if inrange(raceomb_002, 7, 8)

*Sociocultural employment dummy
gen occuSelfDetail_clean = real(subinstr(occuSelfDetail, "-", "", .))
gen SocCulEmployment=.
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 431000, 439000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 131000, 132000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 151000, 153000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 171000, 173000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 471000, 475000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 451000, 459000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 351000, 359000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 371000, 373000)

replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 110000, 119000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 551000, 553000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 511000, 519000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 451000, 459000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 331000, 332000)
replace SocCulEmployment=0 if occuSelfDetail_clean==339000

replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 491000, 499000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 411000, 419000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 191000, 192000)
replace SocCulEmployment=0 if inrange(occuSelfDetail_clean, 531000, 537000)
replace SocCulEmployment=0 if occuSelfDetail_clean==194000

replace SocCulEmployment=1 if inrange(occuSelfDetail_clean, 271000, 274000)
replace SocCulEmployment=1 if inrange(occuSelfDetail_clean, 251000, 259000)
replace SocCulEmployment=1 if inrange(occuSelfDetail_clean, 291000, 292000)
replace SocCulEmployment=1 if inrange(occuSelfDetail_clean, 311000, 319000)
replace SocCulEmployment=1 if inrange(occuSelfDetail_clean, 231000, 232000)
replace SocCulEmployment=1 if inrange(occuSelfDetail_clean, 391000, 399000)
replace SocCulEmployment=1 if inrange(occuSelfDetail_clean, 211000, 212000)
replace SocCulEmployment=1 if occuSelfDetail_clean==333000
replace SocCulEmployment=1 if occuSelfDetail_clean==193000

// Standardize
egen Age = std(age) 

// Create a weight

gen weight = 1

* Code age into categories - the other variables are already in such categories
recode age (min/24=1 "0-24") (25/34=2 "25-34") (35/44=3 "35-44") (45/54=4 "45-54") (55/max=5 "55+"), generate(age_group)

* The below are based on census data, except for ideology, which is based on data from the ANES24 pilot study
gen sextot=.
replace sextot = 0.49 if FemaleGender == 1 // Male
replace sextot = 0.51 if FemaleGender == 2 // Female

gen agetot=.
replace agetot = 0.12 if age_group == 1 // 18-24
replace agetot = 0.17 if age_group == 2 // 25-34
replace agetot = 0.17 if age_group == 3 // 35-44
replace agetot = 0.16 if age_group == 4 // 45-54
replace agetot = 0.38 if age_group == 5 // 55+

gen idtot=.
replace idtot = 0.08 if politicalid_7 == 1 // Strongly conservative
replace idtot = 0.17 if politicalid_7 == 2 // Moderately conservative
replace idtot = 0.08 if politicalid_7 == 3 // Slightly conservative
replace idtot = 0.33 if politicalid_7 == 4 // Neutral
replace idtot = 0.08 if politicalid_7 == 5 // Slightly liberal
replace idtot = 0.18 if politicalid_7 == 6 // Moderately liberal
replace idtot = 0.08 if politicalid_7 == 7 // Strongly liberal

gen edcats=.
replace edcats=1 if inrange(edu_14, 1, 4) // Highschoolandbelow
replace edcats=2 if inrange(edu_14, 5, 6) // Somecollege
replace edcats=3 if edu_14==7 // Bachelors
replace edcats=4 if inrange(edu_14, 8, 14) // Graduate

gen edtot=.
replace edtot = 0.3875 if edcats == 1
replace edtot = 0.2644 if edcats == 2
replace edtot = 0.2208 if edcats == 3
replace edtot = 0.1273 if edcats == 4

gen ethcats=.
replace ethcats=1 if raceomb_002==1 //AmericanIndian/AlaskaNative
replace ethcats=2 if inrange(raceomb_002, 2, 3) //Asian
replace ethcats=3 if raceomb_002==4 //NativeHawaiian
replace ethcats=4 if raceomb_002==5 //Black
replace ethcats=5 if raceomb_002==6 //White
replace ethcats=6 if raceomb_002==7 //Other
replace ethcats=7 if raceomb_002==8 //Multiracial

gen ethtot=.
replace ethtot = 0.01 if ethcats == 1
replace ethtot = 0.0599 if ethcats == 2
replace ethtot = 0.002 if ethcats == 3
replace ethtot = 0.1213 if ethcats == 4
replace ethtot = 0.6051 if ethcats == 5
replace ethtot = 0.0742 if ethcats == 6
replace ethtot = 0.1276 if ethcats == 7

survwgt rake weight , by(FemaleGender age_group politicalid_7 ethcats edcats) totvars(sextot agetot idtot ethtot edtot) generate(rakedweight)

// Regression
regress NonTransphobia Age BIPOC FemaleGender Graduate SocCulEmployment [pweight=rakedweight], robust
eststo
esttab

log close

eststo clear

esttab using "Appendix2.10.rtf", ///
    b(3) se(3) ///
    star(* 0.05 ** 0.01 *** 0.001) ///
    coeflabels(_cons "Constant") ///
    nonumbers replace

// Alternative specification
ologit NonTransphobia Age BIPOC FemaleGender Graduate SocCulEmployment [pweight=rakedweight], robust

